import {
  CoolController,
  BaseController,
  CoolTag,
  TagTypes,
} from '@cool-midway/core';
import { UserInfoEntity } from '../../entity/info';
import { ALL, Body, Inject, Post } from '@midwayjs/core';
import { UserInfoService } from '../../service/info';
import { UserRelationEntity } from '../../entity/user_relation';

/**
 * 用户信息
 */
@CoolController({
  api: ['add', 'delete', 'update', 'info', 'list', 'page'],
  entity: UserInfoEntity,
  service: UserInfoService,
  pageQueryOp: {
    fieldEq: ['a.role', 'a.agentLevel'],
    keyWordLikeFields: ['a.phone'],
    join: [
      {
        entity: UserRelationEntity,
        alias: 'b',
        condition: 'a.id = b.userId',
      },
    ],
    select: ['a.*'],
    addOrderBy: {
      'a.createTime': 'DESC',
    },
    where: async (ctx) => {
      const conditions = [];
      const body = ctx.request.body || {};
      if (body?.order?.prop === 'createTime') {
        body.order.prop = 'a.createTime';
      }
      if (body?.order?.prop === 'updateTime') {
        body.order.prop = 'a.updateTime';
      }
      const { agentId } = ctx.request.body || {};
      if (agentId) {
        conditions.push(['a.role = :role', { role: 2 }]);
        conditions.push([
          '(b.parentId = :agentId OR b.level1ParentId = :agentId OR b.level2ParentId = :agentId OR b.level3ParentId = :agentId)',
          { agentId },
        ]);
      }
      return conditions;
    },
  },
  listQueryOp: {
    fieldEq: ['a.role', 'a.agentLevel'],
    keyWordLikeFields: ['a.phone'],
    join: [
      {
        entity: UserRelationEntity,
        alias: 'b',
        condition: 'a.id = b.userId',
      },
    ],
    select: ['a.*'],
    addOrderBy: {
      'a.createTime': 'DESC',
    },
    where: async (ctx) => {
      const conditions = [];
      const body = ctx.request.body || {};
      if (body?.order?.prop === 'createTime') {
        body.order.prop = 'a.createTime';
      }
      if (body?.order?.prop === 'updateTime') {
        body.order.prop = 'a.updateTime';
      }
      const { agentId } = ctx.request.body || {};
      if (agentId) {
        conditions.push(['a.role = :role', { role: 2 }]);
        conditions.push([
          '(b.parentId = :agentId OR b.level1ParentId = :agentId OR b.level2ParentId = :agentId OR b.level3ParentId = :agentId)',
          { agentId },
        ]);
      }
      return conditions;
    },
  },
})
export class AdminUserInfoController extends BaseController {
  @Inject()
  ctx;

  @Inject()
  userInfoService: UserInfoService;

  // 创建商户
  @Post('/createMerchant', { summary: '创建商户' })
  async createMerchant(@Body(ALL) params: any) {
    return this.ok(await this.userInfoService.createMerchant(params));
  }

  // 申请代理
  @CoolTag(TagTypes.IGNORE_TOKEN)
  @Post('/createAgent', { summary: '申请代理' })
  async createAgent(@Body(ALL) params: any) {
    return this.ok(await this.userInfoService.createAgentNew(params));
  }

  // 发送修改密码短信
  @CoolTag(TagTypes.IGNORE_TOKEN)
  @Post('/getResetPhoneCode', { summary: '发送修改密码短信' })
  async getResetPhoneCode(@Body(ALL) params: any) {
    return this.ok(await this.userInfoService.getResetPhoneCode());
  }
}
